clear matrix
clear
set mem 256m
set matsize 500

// Uncomment the next line and set the path to the directory containing this do file (without trailing slash).
*global repos "path/to/here"

include "./data_setup.do"

merge 1:1 country year using "$repos/data/bremenprivatization/schmitt2011.table1.dta"
drop _merge
drop if ccode == .

tsset ccode year

// Better units for regression table display
replace privschmitt = 100*privschmitt
gen privschmitt_d1 = -d.privschmitt
gen privschmitt_l1 = l.privschmitt
label variable privschmitt_l1 "$ SOE_{i,t-1} $" 



//// Estimate models
xttobit pcd_priv_rev pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_m pubdebt_l1_md gfd_mkcap_pc_l1_m gfd_mkcap_pc_l1_md govfrac_m govfrac_md govleft prgal gl_x_prgal, ll(0)
// Save the frequencies of the distribution of this variable for later overlay-plotting on the marginal effects plots.
summ prgal if e(sample)
// N.B. if change 2.5 then must change the 1.25 value when plotting marginal effects
egen prgalfreq = cut(prgal), at(75(2.5)100)
tab prgalfreq if e(sample), matcell(PRFreq) matrow(PRVals)


xttobit  pcd_priv_rev  pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_md gfd_mkcap_pc_l1_md govfrac_md govleft prgal gl_x_prgal, ll(0)
local N_g = e(N_g)
xi: tobit  pcd_priv_rev i.ccode pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_md gfd_mkcap_pc_l1_md govfrac_md govleft prgal gl_x_prgal, ll(0)
estadd scalar N_x = `N_g'
estimates store PrivFEPR
summ prgal if e(sample)
local PrivFEPR_min `r(min)'
local PrivFEPR_max `r(max)'
// Estimate the substantive effect size of a shift from low to high wcoord
lincom _b[govleft] + _b[gl_x_prgal]*`PrivFEPR_min'
local MFXgllo `r(estimate)'
lincom _b[govleft] + _b[gl_x_prgal]*`PrivFEPR_max'
local MFXglhi `r(estimate)'
summ govleft if e(sample)
local gl_sd `r(sd)'
local MFXgl = round(`gl_sd'*(`MFXglhi' - `MFXgllo'),5)
capture {
  file open mfxfile using "$repos/writing/Privatization/empirical/PrivFEPR_mfxgl.tex", write replace
  file write mfxfile "`MFXgl'"
  file close mfxfile
}
summ pcd_priv_rev if e(sample)
local priv_sd = round(r(sd),5)
capture {
  file open privfile using "$repos/writing/Privatization/empirical/privrev_sd.tex", write replace
  file write privfile "`priv_sd'"
  file close privfile
}
summ pubdebt_l1 if e(sample)
local pubdebt_sd = r(sd)
local MFXpubdebt = round(_b[pubdebt_l1]*`pubdebt_sd',5)
capture {
  file open pubdebtfile using "$repos/writing/Privatization/empirical/PrivFEPR_mfxpubdebt.tex", write replace
  file write pubdebtfile "`MFXpubdebt'"
  file close pubdebtfile
}

// Conduct LM test of autocorrelation and place the t-statistic of the lagged-residual coefficient in a file to be read into the LaTeX doc.
predict yhat
gen resid = pcd_priv_rev - yhat
xi: tobit  pcd_priv_rev l.resid i.ccode pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_md gfd_mkcap_pc_l1_md govfrac_md govleft prgal gl_x_prgal, ll(0)
lincom _b[l.resid]
local tstat = round(r(estimate)/r(se),.01)
capture {
  file open tstatfile using "$repos/writing/Privatization/empirical/PrivFEPR_resid_tstat.tex", write replace
  file write tstatfile "`tstat'"
  file close tstatfile
}
drop yhat resid

xi: tobit pcd_priv_rev i.ccode pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_md gfd_mkcap_pc_l1_md govfrac_md govleft prgal, ll(0)
estadd scalar N_x = `N_g'
estimates store PrivFEPRd

xttobit pcd_priv_rev pcd_cum_rev_l1 govleft prgal gl_x_prgal, ll(0)
local N_g = e(N_g)
xi: tobit pcd_priv_rev i.ccode pcd_cum_rev_l1 govleft prgal gl_x_prgal, ll(0)
estadd scalar N_x = `N_g'
estimates store PrivFEPRnc

xttobit pcd_priv_rev pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_m pubdebt_l1_md gfd_mkcap_pc_l1_m gfd_mkcap_pc_l1_md govfrac_m govfrac_md govleft prgal gl_x_prgal, ll(0)
local N_g = e(N_g)
estadd scalar N_x = `N_g'
estimates store PrivREPR
xttobit pcd_priv_rev pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_m pubdebt_l1_md gfd_mkcap_pc_l1_m gfd_mkcap_pc_l1_md govfrac_m govfrac_md govleft_m govleft_md prgal_m prgal_md gl_x_prgal_m gl_x_prgal_md, ll(0)
local N_g = e(N_g)
estadd scalar N_x = `N_g'
estimates store PrivREPRwb
jackknife, cluster(ccode): xttobit pcd_priv_rev pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1 gfd_mkcap_pc_l1 govfrac govleft prgal gl_x_prgal, ll(0)
local N_g = e(N_g)
estadd scalar N_x = `N_g'
estimates store PrivREPRJack

// Reviewer robustness
xi: tobit  pcd_priv_rev i.ccode pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_md gfd_mkcap_pc_l1_md govfrac_md govleft prgal gl_x_prgal wcoord gl_x_wcoord, ll(0)
estadd scalar N_x = `N_g'
estimates store PrivFEWC
xi: tobit  pcd_priv_rev i.ccode pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_md gfd_mkcap_pc_l1_md govfrac_md govleft prgal gl_x_prgal uden_l1 gl_x_udenl1, ll(0)
estadd scalar N_x = `N_g'
estimates store PrivFEUDen


xttobit  pcd_priv_rev pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_md gfd_mkcap_pc_l1_md govfrac_md govleft prgal gl_x_prgal effpar_leg gl_x_effparleg, ll(0)
local N_g = e(N_g)
xi: tobit  pcd_priv_rev i.ccode pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_md gfd_mkcap_pc_l1_md govfrac_md govleft prgal gl_x_prgal effpar_leg gl_x_effparleg, ll(0)
estadd scalar N_x = `N_g'
estimates store PrivFEEffpar
xttobit  pcd_priv_rev pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_md gfd_mkcap_pc_l1_md govfrac_md govleft prgal gl_x_prgal socexp_t_pmp_l1 gl_x_socexpl1, ll(0)
local N_g = e(N_g)
xi: tobit  pcd_priv_rev i.ccode pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_md gfd_mkcap_pc_l1_md govfrac_md govleft prgal gl_x_prgal socexp_t_pmp_l1 gl_x_socexpl1, ll(0)
estadd scalar N_x = `N_g'
estimates store PrivFESocexp
xttobit  pcd_priv_rev  pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_md gfd_mkcap_pc_l1_md govfrac_md govleft prgal gl_x_prgal almp_pmp_l1 gl_x_almpl1, ll(0)
local N_g = e(N_g)
xi: tobit  pcd_priv_rev i.ccode pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_md gfd_mkcap_pc_l1_md govfrac_md govleft prgal gl_x_prgal almp_pmp_l1 gl_x_almpl1, ll(0)
estadd scalar N_x = `N_g'
estimates store PrivFEALMP
// Referee 1 asks if left-privatization is clustered later in the period
xi: tobit  pcd_priv_rev i.ccode pcd_cum_rev_l1 year spatial_pop_l1 spatial_trade_l1 pubdebt_l1_md gfd_mkcap_pc_l1_md govfrac_md govleft prgal gl_x_prgal gl_x_year, ll(0)
estadd scalar N_x = `N_g'
estimates store PrivFEYear


xttobit  pcd_priv_rev  pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_md gfd_mkcap_pc_l1_md govfrac_md govleft prrel gl_x_prrel, ll(0)
local N_g = e(N_g)
xi: tobit  pcd_priv_rev i.ccode pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_md gfd_mkcap_pc_l1_md govfrac_md govleft prrel gl_x_prrel, ll(0)
estadd scalar N_x = `N_g'
estimates store PrivFEPRRel

xttobit  pcd_priv_rev  pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_md gfd_mkcap_pc_l1_md govfrac_md govleft prabso gl_x_prabso, ll(0)
local N_g = e(N_g)
xi: tobit  pcd_priv_rev i.ccode pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_md gfd_mkcap_pc_l1_md govfrac_md govleft prabso gl_x_prabso, ll(0)
estadd scalar N_x = `N_g'
estimates store PrivFEPRAbs

xttobit  pcd_priv_rev  pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_md gfd_mkcap_pc_l1_md govfrac_md govleft logmdmh gl_x_logmdmh, ll(0)
local N_g = e(N_g)
xi: tobit  pcd_priv_rev i.ccode pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_md gfd_mkcap_pc_l1_md govfrac_md govleft logmdmh gl_x_logmdmh, ll(0)
estadd scalar N_x = `N_g'
estimates store PrivFEDM

xttobit  privschmitt_d1 privschmitt_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1 gfd_mkcap_pc_l1 govfrac govleft prgal gl_x_prgal, ll(0)
local N_g = e(N_g)
tobit  privschmitt_d1 privschmitt_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1 gfd_mkcap_pc_l1 govfrac govleft prgal gl_x_prgal, ll(0)
estadd scalar N_x = `N_g'
estimates store PrivFEPS


local out_dir "$repos/writing/Privatization/empirical/"
local estout_options label replace stats(N N_x N_lc, labels("N" "N countries" "Censored")) style(tex) posthead("\hline") prefoot("\hline") postfoot("\hline")

// Main results
*estout  PrivFEWCd    PrivFEWC     PrivFEWCnc	using "`out_dir'/models_priv_fe.tex",			cells( " b( fmt(a2) ) se(fmt(3)) p(fmt(2)) " ) drop(_cons _Icco*)	`estout_options' order(pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_md gfd_mkcap_pc_l1_md year govfrac_md wcoord govleft gl_x_wcoord) mlabels("(M1)" "(M2)" "(M3)" "(M4)", nodepvars)
estout  PrivFEPRd    PrivFEPR     PrivREPRJack 	using "`out_dir'/models_priv_pr_fe.tex",		cells( " b( fmt(a2) ) se(fmt(3)) p(fmt(2)) " ) drop(_cons _Icco*)	`estout_options' mlabels("(B1)" "(B2)" "(B3)", nodepvars) rename(pubdebt_l1 pubdebt_l1_md gfd_mkcap_pc_l1 gfd_mkcap_pc_l1_md govfrac govfrac_md)
estout  PrivREPRwb   PrivFEPRnc   PrivFEYear	using "`out_dir'/models_priv_pr_re.tex",		cells( " b( fmt(a2) ) se(fmt(3)) p(fmt(2)) " ) drop(_cons _Icco*)	`estout_options' mlabels("(B4)" "(B5)" "(B6)", nodepvars) rename(prgal_md prgal govleft_md govleft gl_x_prgal_md gl_x_prgal) order(pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_m pubdebt_l1_md gfd_mkcap_pc_l1_m gfd_mkcap_pc_l1_md govfrac_m govfrac_md govleft_m govleft prgal_m prgal gl_x_prgal_m gl_x_prgal)
estout  PrivFEEffpar PrivFESocexp PrivFEALMP	using "`out_dir'/models_priv_rob_fe.tex",		cells( " b( fmt(a2) ) se(fmt(3)) p(fmt(2)) " ) drop(_cons _Icco*)	`estout_options' mlabels("(B7)" "(B8)" "(B9)", nodepvars)
estout  PrivFEWC     PrivFEUDen   PrivFEYear	using "`out_dir'/models_priv_union_fe.tex",		cells( " b( fmt(a2) ) se(fmt(3)) p(fmt(2)) " ) drop(_cons _Icco*)	`estout_options' mlabels("(B10)" "(B11)" "(B12)", nodepvars)
estout  PrivFEPS     PrivFEDM     PrivFEPRRel 	using "`out_dir'/models_priv_morepr_fe.tex",	cells( " b( fmt(a2) ) se(fmt(3)) p(fmt(2)) " ) drop(_cons _Icco*)	`estout_options' mlabels("(B13)" "(B14)" "(B15)", nodepvars) rename(govfrac_md govfrac pubdebt_l1_md pubdebt_l1 gfd_mkcap_pc_l1_md gfd_mkcap_pc_l1 prgal_md prgal govleft_md govleft gl_x_prgal_md gl_x_prgal) order(privschmitt_l1 pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1 gfd_mkcap_pc_l1 govfrac govleft prgal gl_x_prgal)



tobit pcd_priv_rev gl_x_prgal i.ccode pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1 gfd_mkcap_pc_l1 govfrac govleft prgal, ll(0)
estimates store JackFull
*local matV = e(V)
local CoefFull = _b[gl_x_prgal]
local sumSqCoefDiff = 0
foreach cty in Austria Belgium Denmark Finland France Germany Greece Ireland Italy Netherlands Portugal Spain Sweden UK {
  tobit pcd_priv_rev gl_x_prgal i.ccode pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1 gfd_mkcap_pc_l1 govfrac govleft prgal if country != "`cty'", ll(0)
  estimates store Jack`cty'
  local sumSqCoefDiff = `sumSqCoefDiff' + (_b[gl_x_prgal] - `CoefFull')^2
}
local jackCoefSE = (14-1)/14 * `sumSqCoefDiff'
di `sumSqCoefDiff'^(1/2)
estimates restore JackFull
estimates





/// Write out the processed data for plotting in R
keep country year pcd_priv_rev pcd_cum_rev_l1 spatial_pop_l1 spatial_trade_l1 pubdebt_l1_md gfd_mkcap_pc_l1_md govfrac_md govleft wcoord gl_x_wcoord pcd_cum_rev_left pcd_cum_rev prgal gl_x_prgal
save "$repos/writing/Privatization/empirical/processed.dta", replace

/// Write out collapsed data for plotting in R
gen glwcoord = wcoord if govleft > 0
collapse (mean) govleft wcoord glwcoord (max) pcd_cum_rev pcd_cum_rev_left, by(country)
save "$repos/writing/Privatization/empirical/ctryaggs.dta", replace

//// Plotting conditional effects

// Clear old data
drop _all
//do "`base_dir'\empirical\marginal_effects_plot_helper.do"
// Generate new variables
gen model = ""
* Unified
gen       cgl = .
label var cgl "Conditional Partisanship Effect"
gen       cgl_upper95 = .
label var cgl_upper95 "Upper bound (95%)"
gen       cgl_lower95 = .
label var cgl_lower95 "Lower bound (95%)"
* Conditioning variable
gen lambda = .
gen lambda_counter = .

local models PrivFEPR

// Iterate over the stored models and fill up our data set
// Get ourselves the first (empty) observation
set obs 1
foreach model_name in `models' {
  estimates restore `model_name'
  replace model = "`model_name'" in l
  if "`model_name'" == "PrivFEPR" {
    local conditional "gl_x_prgal"
    local lambda_min    = round(`PrivFEPR_min', 1)
    local lambda_max    = round(`PrivFEPR_max', 1)
  }
  local lambda_mean    = (`lambda_max' - `lambda_min')/2
  local lambda_diff    = (`lambda_max' - `lambda_min')/10
  local counter = 1
  replace lambda_counter = `counter' in l
  forvalues intcount = 0/10 {
    local lambda_level = `lambda_min' + `intcount'*`lambda_diff'
    nlcom _b[govleft] + (_b[`conditional']*`lambda_level')
    local val el(r(b), 1, 1)
    local se  el(r(V), 1, 1)^(1/2)
    replace cgl = `val' if lambda_counter == `counter' & model == "`model_name'"
    replace cgl_upper95 = `val' + `se'*1.96 if lambda_counter == `counter' & model == "`model_name'"
    replace cgl_lower95 = `val' - `se'*1.96 if lambda_counter == `counter' & model == "`model_name'"
    replace lambda = `lambda_level' if lambda_counter == `counter' & model == "`model_name'"
    local counter = `counter' + 1
    local new = _N + 1
    set obs `new'
    replace model = "`model_name'" in l
	replace lambda_counter = `counter' in l
	}
}
// Get rid of the erroneously-added final observation.
drop in l

svmat PRFreq, names(prgal)
svmat PRVals, names(prgalvals)
replace prgalvals = prgalvals + 1.25

local models PrivFEPR
foreach model_name in `models' {
  if "`model_name'" == "PrivFEPR" {
    label var lambda  "Electoral proportionality"
	local lambdavar prgal1
  }
  twoway  (bar `lambdavar' prgalvals, barwidth(2.5) ytitle("Frequency", axis(2)) yaxis(2) yscale(alt  axis(2)) color(gs14)) (line cgl lambda, yaxis(1) yscale(alt axis(1)) lpattern(solid) yline(0) lcolor(gs0)) (line cgl_upper95 lambda, yaxis(1) yscale(alt axis(1)) lpattern(dash) lcolor(gs0)) (line cgl_lower95 lambda, yaxis(1) yscale(alt axis(1)) lpattern(dash) lcolor(gs0)) if model == "`model_name'", legend(off) ytitle("Partisanship Effect", axis(1)) xtitle("Electoral Proportionality") scheme(s1mono) yscale(range(-6 (1) 2)) ylabel(-6 (1) 2)
  graph export "$repos/writing/Privatization/images/conditional_effects_`model_name'.pdf", replace
  graph export "$repos/writing/Privatization/images/conditional_effects_`model_name'.png", replace
}
window manage close graph
